package cn.buildoncloud.labor.workermanapplet.business.modules.services.impl;

import cn.buildoncloud.labor.workermanapplet.business.modules.services.LaborServiceCompanyService;
import cn.buildoncloud.labor.workermanapplet.dto.LaborServiceCompanyDTO;
import cn.buildoncloud.labor.workermanapplet.util.StringUtil;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.dao.entity.Record;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: cn.buildoncloud.labor.workermanapplet.business.modules.services.impl
 * Created by CuiZibo on 2018/11/13 11:27
 */
@Service
public class LaborServiceCompanyServiceImpl implements LaborServiceCompanyService {
    public LaborServiceCompanyServiceImpl(Dao dao) {
        this.dao = dao;
    }

    private Dao dao;

    @Override
    public List<LaborServiceCompanyDTO> listByUnit(String unitId, String key) {
        List<LaborServiceCompanyDTO> list = new ArrayList<>();

        Cnd cnd = Cnd.where("delFlag", "=", false);
        if (StringUtil.isNotBlank(key)) {
            SqlExpressionGroup sqlExpressionGroup = new SqlExpressionGroup();
            sqlExpressionGroup.or("name", "like", "%" + key + "%")
                    .or("contact_tel", "like", "%" + key + "%")
                    .or("contact", "like", "%" + key + "%")
            ;
            cnd.and(sqlExpressionGroup);
        }
        Record fetch = dao.fetch("sys_unit", Cnd.where("parentId", "is", null));
        if (Long.parseLong(unitId) != fetch.getLong("id")) {
            cnd.and("unit_id", "=", unitId);
        }

        List<Record> recordList = dao.query("bi_labor_service_company", cnd);
        recordList.stream().forEach(item -> {
            Record fetch1 = dao.fetch("sys_unit", Cnd.where("id", "=", item.getLong("unit_id")));

            LaborServiceCompanyDTO l = new LaborServiceCompanyDTO();
            l.setId(item.getLong("id")+"");
            l.setName(item.getString("name"));
            l.setBankAccount(item.get("bank_account").toString());
            l.setOpenAccountBank(item.get("open_account_bank").toString());
            l.setContactTel(item.get("contact_tel").toString());
            l.setDutyNum(item.get("duty_num").toString());
            l.setUnitId(item.getLong("unit_id")+"");
            l.setUnitName(fetch1.getString("name"));
            l.setContact(item.get("contact").toString());
            l.setIsBigClearPack((int)item.get("is_bigclearpack"));
            l.setMaterialIs((int)item.get("material_is"));
            List<String> fileIds = new ArrayList<>();
            List<Record> records = dao.query("bi_labor_service_company_qualification", Cnd.where("labor_service_company_id", "=", item.get("id")));
            records.stream().forEach(i -> {
                fileIds.add(i.get("fileUploadInfo_id").toString());
            });
            l.setQualificationIdList(fileIds);
            list.add(l);
        });
        return list;
    }

    /**
     *@author gnaf0312@163.com
     *@create 2019/1/21 16:59
     *@Param [dutyNum, unitId]
     *@return java.lang.Object
     *@desc 根据税号与公司id 查询
     */
    @Override
    public boolean selectLaborServiceCompanyByDutyNumAndUnitId(String dutyNum, String unitId,String name) {
        SqlExpressionGroup e = new SqlExpressionGroup();
        e.and("duty_num","=",dutyNum).or("name","=",name);
        Condition c = Cnd.where("delFlag","=",false).and("unit_id","=",unitId).and(e);
        Record record = dao.fetch("bi_labor_service_company",c);
        return  record==null?false:true;

    }

    /**
     *@author maojunfang  gnaf0312@163.com
     *@create 2019/1/22 15:11
     *@Param [unitId, name, dutyNum]
     *@return boolean
     *@desc  判断当前公司下  是否有符合条件的劳务公司
     */
    @Override
    public boolean judgeNameOrDutyNum(String unitId, String name , String dutyNum) {
        Cnd c = Cnd.where("delFlag", "=", false).and("unit_id", "=", unitId);
        //验证名称
        if(name!=null){
            c.and("name", "=", name);
        //  验证验证码
        }else if(dutyNum !=null){
            c.and("duty_num", "=", dutyNum);
        }
        Record record = dao.fetch("bi_labor_service_company", c);
        return record==null?false:true;
    }

}
